import { defineStore } from "pinia";
import { ref, reactive, computed } from "vue";
// import { login as loginApi } from "@/apis";
import { setToken, clearToken, getToken } from "@/utils/auth";

const storeSetup = () => {
  const userInfo = reactive({
    name: "",
    avatar: ""
  });
  const userName = computed(() => userInfo.name);
  const avatar = computed(() => userInfo.avatar);

  const token = ref(getToken() || "");

  // 重置token
  const resetToken = () => {
    token.value = "";
    clearToken();
  };

  // 登录
  const login = async (params) => {
    // const res = await loginApi(params);
    // setToken(res.data.token);
    // token.value = res.data.token;
  };

  // 退出
  const logout = () => {
    token.value = "";
    clearToken();
  };

  // 获取用户信息
  const getInfo = () => {
    userInfo.name = "管理员";
    userInfo.avatar =
      "https://images.maishou88.com/admin/union_activities/20230828/20230828143248_5393.jpg";
  };

  return {
    userInfo,
    userName,
    avatar,
    token,
    login,
    logout,
    getInfo,
    resetToken
  };
};

export const useUserStore = defineStore("user", storeSetup, {
  persist: { paths: ["token"], storage: localStorage }
});
